


from analyse.kline_handler.composition import KlineComposition
from analyse.kline_handler.enum import RiseAndFallType, ShapeType
from analyse.kline_handler.fall.fall_handler import FallHandler
from analyse.kline_handler.flat.flat_handler import FlatHandler
from analyse.kline_handler.handler import KlineHandler
from analyse.kline_handler.rise.rise_handler import RiseHandler
from analyse.kline_handler.shadow import ShadowHandler
from analyse.kline_handler.shape.large import LargeHandler
from analyse.kline_handler.shape.line import LineHandler
from analyse.kline_handler.shape.medium import MediumHandler
from analyse.kline_handler.shape.small import SmallHandler
from analyse.kline_handler.shape.star import StarHandler


class RiseAndFallHandler(KlineHandler):
    def handle(self, kline):
        if kline["suspendFlag"]==1:
            return {**kline}
        change = kline["close"]-kline["preClose"]
        chg=change/kline["preClose"]*100
        amplitude = (kline["high"]-kline["low"])/kline["preClose"]*100
        # 计算上影线、下影线、实体长度
        new_kline=KlineComposition().handle({**kline, "change":change, "chg":chg, "amplitude":amplitude,"dec":[]})
        ShadowHandler().handle(new_kline)
        if kline["close"]>kline["preClose"]:
            self.compound_kline_type(new_kline,RiseAndFallType["上涨"].value*1000000)
            # return RiseHandler().handle(new_kline)
        elif kline["close"]<kline["preClose"]:
            self.compound_kline_type(new_kline,RiseAndFallType["下跌"].value*1000000)
            # return FallHandler().handle(new_kline)
        else:
            self.compound_kline_type(new_kline,RiseAndFallType["平盘"].value*1000000)
            # return FlatHandler().handle(new_kline)
        
        kline_type=self.get_kline_type(new_kline)
        if kline_type["shape_type"]==ShapeType.一字.value:
            LineHandler().handle(new_kline)
        elif kline_type["shape_type"]==ShapeType.星线.value:
            StarHandler().handle(new_kline)
        elif kline_type["shape_type"]==ShapeType.小.value:
            SmallHandler().handle(new_kline)
        elif kline_type["shape_type"]==ShapeType.中.value:
            MediumHandler().handle(new_kline)
        elif kline_type["shape_type"]==ShapeType.大.value:
            LargeHandler().handle(new_kline)

        return new_kline